<?php

namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use App\Model\Admin\Article;
use App\Model\Admin\Category;

class ArticleController extends CommonController
{
		// get.admin/article   全部文章列表显示
    public function index(){
        $data = Article::paginate(2);
    	return view('admin.article.index',compact('data'));
    }

    // get.admin/article/create  添加文章界面显示
    public function create(){
    	$data = (new Category)->tree();
    	return view('admin.article/add',compact('data'));
    }

    // post.admin/article 文章添加数据存储
    public function store(Request $request){
    	$input = Input::except('_token','file_upload');
        $rules = [
            'art_title'    =>  'required',
            'art_content'  =>  'required',
        ];
        $message = [
            'art_title.required'     =>  '文章标题不能为空',
            'art_content.required'   =>  '文章内容不能为空',
        ];
        // 验证
        $validator = Validator::make($input, $rules, $message);
        if($validator->passes()){
            // 获取文件对象
            $file = $request->file('file_upload');
            if($file){ //判断是否有文件被上传
                if($file->isvalid()){ // 判断文件是否上传成功
                    // 文件上传方法，传入文件对象
                    $data = (new Article)->upload($file);
                    $input['art_thumb'] = $data['art_thumb'];
                    $input['art_time'] = time();
                    $result = Article::create($input);
                    if($result){
                        return redirect('admin/article')->with('success', '文章添加成功！');;
                    }else{
                        return back()->with('errors','文章添加失败！');
                    }
                }
            }else{
                return back()->with('errors','没有图片被上传，请选择一张图片！');
            }  
        }else{
            return back()->withErrors($validator);
        }  	
    }

    // admin/article/{article}/edit  编辑页面的显示
    public function edit($art_id){
        $article = Article::find($art_id);
        $data = (new Category)->tree();
        return view('admin.article.edit',compact('article','data'));
    } 

    // put admin/article/{article}  文章的数据修改
    public function update(Request $request,$art_id){
        $input = Input::except('_token','_method','file_upload');
        $article = new Article;
        $art = $article->find($art_id);
        if($art->art_thumb){
            $path = "";
            @unlink($path);
        }
        // 获取文件对象
        $file = $request->file('file_upload');
        if($file){
            // 文件上传方法，传入文件对象
            $data = $article->upload($file);
            // 判断文件是否上传成功
            if(isset($data['art_thumb'])){
                $input['art_thumb'] = $data['art_thumb'];
            }
        }
        $result = $article->where('art_id',$art_id)->update($input);
        if($result !== false){
            return redirect('admin/article')->with('success','文章修改成功！');
        }else{
            return back()->with('errors','文章修改失败！');
        }
    }

    // delete.admin/category/{category}
    public function destroy($art_id){
        $input = Input::except('_token','_method');
        $result = Article::where('art_id',$art_id)->delete();
        if($result){
            $data = [
                'status'  =>   '1',
                'msg'     =>   '分类删除成功',
            ];
        }else{
            $data = [
                'status'  =>   '0',
                'msg'     =>   '分类删除失败',
            ];
        }
        return $data;
    }

}
